﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using BusinessObject;
using Utility;
using System.Web.Security;
using System.Resources;
namespace WebTeamplate
{
    public partial class Admin_DTBanHang : BasePage
    {
        #region Fields
        public  int tongSoPhieu = 0;
        public double tongTien = 0;
        public string thanhtienText = string.Empty;
        public string thanhtienNumber = string.Empty;
        public string linkDS = string.Empty;
        #endregion

        #region Init,Load
        protected void Page_Load(object sender, EventArgs e)
        {
            ClientDefaultButton.SetPageDefaultButton(this.Page, btnSearch.ClientID);
            if (!Page.IsPostBack)
            {
                InitData();
                LoadData();
            }

        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitTextControl();      
        }
      
        #endregion

        #region Methods

        public void InitTextControl()
        {
            btnSearch.Text = "Thống kê";
            btnReset.Text = Resources.Resource.LABEL_BUTTON_RESET;        
        }
        private void InitData()
        {
            vsumSumary.ValidationGroup = cusvNgayLapPhieuFrom.ValidationGroup = cusvNgayLapPhieuTo.ValidationGroup = btnSearch.ValidationGroup = "phieuhang";
            cusvNgayLapPhieuFrom.ErrorMessage = cusvNgayLapPhieuTo.ErrorMessage = string.Format(Resources.Resource.MESSAGE_VALIDATE_INPUT, CommonFunction.GetTextLabel(lblNgayNhap));
            cusvNgayLapPhieuFrom.ValidationGroup = cusvNgayLapPhieuTo.ValidationGroup = "phieuhang";
            CommonFunction.LoadSubCate(drlKho, CommonFunction.DEFAULT_WAREHOUSE_CODE, true);

        }


        public void LoadData()
        {

            string condition = " where [dbo].[fcCurrencyInPhieuBanHang]('" + CommonFunction.DEFAULT_VND_CURENCY + "',ph.MaPhieuBanHang)=1";            
            if (drlKho.SelectedValue != string.Empty)
                condition += " and ph.maKho=" + drlKho.SelectedValue;
            if (txtMaMatHang.Text != string.Empty)
                condition += " and [dbo].[fcMaMatHangInPhieuBanHang]('" + txtMaMatHang.Text.Trim() + "',ph.MaPhieuBanHang)=1";
            if (txtMaPhuTung.Text != string.Empty)
                condition += " and [dbo].[fcMaPhuTungInPhieuBanHang]('" + txtMaPhuTung.Text.Trim() + "',ph.MaPhieuBanHang)=1";
            if (txtNgayLapPhieuFrom.Text != string.Empty)
            {
                DateTime dt = ConvertValueObject.ParseDateTime(txtNgayLapPhieuFrom.Text);
                condition += " and DATEDIFF(day,'" + dt.Year + "-" + dt.Month + "-" + dt.Day + "',ph.NgayLapPhieu)>=0";
            }
            if (txtNgayLapPhieuTo.Text != string.Empty)
            {
                DateTime dt = ConvertValueObject.ParseDateTime(txtNgayLapPhieuTo.Text);
                condition += " and DATEDIFF(day,'" + dt.Year + "-" + dt.Month + "-" + dt.Day + "',ph.NgayLapPhieu)<=0";
            }            
            MatHangTonKhoUtil.ThongKeDoanhThuBanHang(condition, ref tongSoPhieu, ref tongTien);
            thanhtienNumber = ConvertValueObject.ParseString(tongTien);
            thanhtienText = CommonFunction.ReadNumber(thanhtienNumber) + " " + CommonFunction.DEFAULT_VND_CURENCY;
            //CommonFunction.ReadNumber(ConvertValueObject.ParseString(total)) + " " + objCurency.Name;
            linkDS = "<a href='DMPhieuBanHang.aspx?kho=" + drlKho.SelectedValue + "&amp;mamh=" + txtMaMatHang.Text+ "&amp;mapt=" + txtMaPhuTung.Text + "&amp;sdate=" + txtNgayLapPhieuFrom.Text + "&amp;edate=" + txtNgayLapPhieuTo.Text + "'>Xem DS phiếu báo giá</a>";
        }



        #endregion

        #region Events,Handler


        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();
        }


        #endregion
    }
}
